import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'sunmi_nfc_manager_platform_interface.dart';

/// An implementation of [SunmiNfcManagerPlatform] that uses method channels.
class MethodChannelSunmiNfcManager extends SunmiNfcManagerPlatform {
  /// The method channel used to interact with the native platform.
  @visibleForTesting
  final methodChannel = const MethodChannel('sunmi_nfc_manager');

  @override
  Future<bool> initialize() async {
    try {
      final result = await methodChannel.invokeMethod<bool>('initialize');
      return result ?? false;
    } catch (e) {
      debugPrint(
          'SunmiNfcManagerPlatform--->Error initializing NFC manager: $e');
      return false;
    }
  }

  @override
  Future<bool> switchNfc(String sn) async {
    try {
      final result = await methodChannel.invokeMethod<bool>('switchNfc', {
        'sn': sn,
      });
      return result ?? false;
    } catch (e) {
      debugPrint('SunmiNfcManagerPlatform--->Error switching NFC: $e');
      return false;
    }
  }

  @override
  Future<bool> setNfcWaterMarkAlpha(int alpha) async {
    try {
      final result =
          await methodChannel.invokeMethod<bool>('setNfcWaterMarkAlpha', {
        'alpha': alpha,
      });
      return result ?? false;
    } catch (e) {
      debugPrint(
          'SunmiNfcManagerPlatform--->Error setting watermark alpha: $e');
      return false;
    }
  }

  @override
  Future<bool> unregisterNfcListener() async {
    try {
      final result =
          await methodChannel.invokeMethod<bool>('unregisterNfcListener');
      return result ?? false;
    } catch (e) {
      debugPrint(
          'SunmiNfcManagerPlatform--->Error unregistering NFC listener: $e');
      return false;
    }
  }

  @override
  Future<bool> destroy() async {
    try {
      final result = await methodChannel.invokeMethod<bool>('destroy');
      return result ?? false;
    } catch (e) {
      debugPrint('SunmiNfcManagerPlatform--->Error destroying NFC manager: $e');
      return false;
    }
  }

  @override
  Future<List<Map<dynamic, dynamic>>> getCurrentNfcList() async {
    try {
      final result =
          await methodChannel.invokeMethod<List>('getCurrentNfcList');
      return (result ?? []).cast<Map<dynamic, dynamic>>();
    } catch (e) {
      debugPrint(
          'SunmiNfcManagerPlatform--->Error getting current NFC list: $e');
      return [];
    }
  }
}
